import axios from 'axios';
import { message } from 'antd';

// 创建axios实例
const api = axios.create({
  baseURL: window.env?.BACKEND_URL || 'http://127.0.0.1:8199',
  timeout: 100000,
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    const { data } = response;
    if (data.code === 200) {
      return data;
    } else {
      message.error(data.message || '请求失败');
      return Promise.reject(new Error(data.message || '请求失败'));
    }
  },
  (error) => {
    message.error(error.message || '网络错误');
    return Promise.reject(error);
  }
);

// C端API
export const clientApi = {
  // 获取菜品列表
  getDishes: (params) => api.get('/client/dish/list', { params }),
  
  // 获取菜品详情
  getDishDetail: (id) => api.get(`/client/dish/${id}`),
  
  // 获取推荐菜品
  getRecommendedDishes: () => api.get('/client/dish/recommended'),
  
  // 获取随机菜品列表
  getRandomDishes: (categoryIds, tagIds, dishIds) => {
    const params = {};
    if (categoryIds && categoryIds.length > 0) params.categoryIds = categoryIds;
    if (tagIds && tagIds.length > 0) params.tagIds = tagIds;
    if (dishIds && dishIds.length > 0) params.dishIds = dishIds;
    return api.get('/client/dish/random-list', { params });
  },
  
  // 随机选菜
  randomSelectDish: (data, userId = null, onlyFavorites = false) => {
    const params = {};
    if (userId) params.userId = userId;
    if (onlyFavorites) params.onlyFavorites = onlyFavorites;
    return api.post('/client/dish/random', data, { params });
  },
  
  // 接受菜品
  acceptDish: (userId, dishId) => api.post('/client/dish/selection/accept', {
    userId,
    dishId
  }),
  
  // 拒绝菜品
  rejectDish: (userId, dishId, reason) => api.post('/client/dish/selection/reject', {
    userId,
    dishId,
    reason
  }),
  
  // 获取分类列表
  getCategories: () => api.get('/client/categories'),
  
  // 获取标签列表
  getTags: () => api.get('/client/tags'),
  
  // 用户认证
  register: (data) => api.post('/client/user/register', data),
  login: (data) => api.post('/client/user/login', data),
  
  // 用户信息
  getUserInfo: (id) => api.get(`/client/user/${id}`),
  updateNickname: (userId, nickname) => api.put('/client/user/nickname', {
    userId,
    nickname
  }),
  updatePassword: (userId, oldPassword, newPassword) => api.put('/client/user/password', {
    userId,
    oldPassword,
    newPassword
  }),
  
  // 收藏功能
  addFavorite: (userId, dishId) => api.post('/client/favorite/add', null, {
    params: { userId, dishId }
  }),
  removeFavorite: (userId, dishId) => api.post('/client/favorite/remove', null, {
    params: { userId, dishId }
  }),
  isFavorite: (userId, dishId) => api.get('/client/favorite/check', {
    params: { userId, dishId }
  }),
  getFavoriteList: (userId, page = 1, size = 10) => api.get('/client/favorite/list', {
    params: { userId, page, size }
  }),
  getFavoriteIds: (userId) => api.get('/client/favorite/ids', {
    params: { userId }
  }),
};

// 后台管理API
export const adminApi = {
  // 菜品管理
  dishes: {
    list: (params) => api.get('/admin/dish/list', { params }),
    get: (id) => api.get(`/admin/dish/${id}`),
    create: (data) => api.post('/admin/dish', data),
    update: (id, data) => api.put(`/admin/dish/${id}`, data),
    delete: (id) => api.delete(`/admin/dish/${id}`),
    addTags: (id, tagIds) => api.post(`/admin/dish/${id}/tags`, tagIds),
    removeTags: (id, tagIds) => api.delete(`/admin/dish/${id}/tags`, { data: tagIds }),
  },
  
  // 分类管理
  categories: {
    list: (params) => api.get('/admin/category/list', { params }),
    all: () => api.get('/admin/category/all'),
    get: (id) => api.get(`/admin/category/${id}`),
    create: (data) => api.post('/admin/category', data),
    update: (id, data) => api.put(`/admin/category/${id}`, data),
    delete: (id) => api.delete(`/admin/category/${id}`),
  },
  
  // 标签管理
  tags: {
    list: (params) => api.get('/admin/tag/list', { params }),
    all: () => api.get('/admin/tag/all'),
    get: (id) => api.get(`/admin/tag/${id}`),
    create: (data) => api.post('/admin/tag', data),
    update: (id, data) => api.put(`/admin/tag/${id}`, data),
    delete: (id) => api.delete(`/admin/tag/${id}`),
  },
  
  // AI图片生成
  aiImage: {
    generate: (data) => api.post('/admin/ai-image/generate', data),
    getTaskStatus: (taskId) => api.get(`/admin/ai-image/task/${taskId}`),
    getResult: (taskId) => api.get(`/admin/ai-image/result/${taskId}`),
  },
};

// 数据初始化API
export const initApi = {
  initData: () => api.post('/init/data'),
};

export default api;